﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Posts_Details : BasePostPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
          dsPost.UpdatedData += new EventHandler<Acms.DataControls.UpdatingDataEventArgs<Post>>(dsPost_UpdatedData);
        if (!IsPostBack)
        {
            lnkEdit.Visible = User.HasRole(EditRoles);
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
            if (User.HasRole(Role.Roles.Developer))
            {
                foreach (DataControlField col in dvDetails.Fields)
                {
                    if (col is Acms.WebControls.CheckBoxField)
                    {
                        Acms.WebControls.CheckBoxField tmp = (Acms.WebControls.CheckBoxField)col;
                        if (tmp.DataField.ToLower() == "developerlocked")
                        {
                            tmp.ReadOnly = false;
                        }
                    }
                    else if (col is Acms.WebControls.TextBoxField)
                    {
                        Acms.WebControls.TextBoxField tmp = (Acms.WebControls.TextBoxField)col;
                        if (tmp.DataField.ToLower() == "name")
                        {
                            tmp.ReadOnly = false;
                        }
                    }
                    else if (col is Acms.WebControls.BaseDataField)
                    {
                        Acms.WebControls.BaseDataField tmp = (Acms.WebControls.BaseDataField)col;
                        if (tmp.DataField.ToLower() == "templateid")
                        {
                            tmp.ReadOnly = false;
                        }
                    }
                }
            }
            else if (CurrentPost != null && !CurrentPost.DeveloperLocked)
            {
                foreach (DataControlField col in dvDetails.Fields)
                {
                    if (col is Acms.WebControls.TextBoxField)
                    {
                        Acms.WebControls.TextBoxField tmp = (Acms.WebControls.TextBoxField)col;
                        if (tmp.DataField.ToLower() == "name")
                        {
                            tmp.ReadOnly = false;
                        }
                    }
                }
            }

            //lock homepage naem field
            if (CurrentPost != null && CurrentPost.ParentGuid == null)
            {
                var tmp = dvDetails.Fields.OfType<Acms.WebControls.TextBoxField>().Where(x => x.DataField.ToLower() == "name").SingleOrDefault();
                if (tmp != null)
                {
                    tmp.ReadOnly = true;
                }
            }
        }
    }

    void dsPost_UpdatedData(object sender, Acms.DataControls.UpdatingDataEventArgs<Post> e)
    {
        base.CurrentPostUpdated(e.Item);
    }

    protected override string PageTitle
    {
        get { return "Details"; }
    }
    private Role.Roles[] EditRoles = new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher }; 

    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentViewer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher }; }
    }
    protected void dsPost_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(base.CurrentPost);
    }

    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        dvDetails.ChangeMode(DetailsViewMode.Edit);
        lnkEdit.Visible = false;
        lnkSave.Visible = true;
        lnkCancel.Visible = true;
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        dvDetails.UpdateItem(true);

        lnkEdit.Visible = User.HasRole(EditRoles);
        lnkSave.Visible = false;
        lnkCancel.Visible = false;

        ucHeader.UpdateHeader();

    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        dvDetails.ChangeMode(DetailsViewMode.ReadOnly);
        lnkEdit.Visible = User.HasRole(EditRoles);
        lnkSave.Visible = false;
        lnkCancel.Visible = false;

    }

    protected void dsTemplates_SelectData(object sender, Acms.DataControls.SelectEventArgs<Template> e)
    {
        e.SetData(Template.GetAllBySiteId(AcmsContext.SiteId));
    }
    protected void dsPost_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Post> e)
    {
        e.Item.RequiresPublishing = true;
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Details");
    }

}
